package com.easylinkin.linkappapi.progress.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.common.utils.excel.ExcelConstant;
import com.easylinkin.linkappapi.common.utils.excel.ExcelTools;
import com.easylinkin.linkappapi.common.utils.io.OutputStreamUtil;
import com.easylinkin.linkappapi.progress.entity.dto.ProgressInfoDTO;
import com.easylinkin.linkappapi.progress.entity.dto.ScreenProgressInfoDTO;
import com.easylinkin.linkappapi.progress.entity.vo.ProgressInfoVo;
import com.easylinkin.linkappapi.progress.service.ProgressInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * 进度管理/进度计划
 *
 * @author zhengwen
 * @date 2023/11/13
 */
@Slf4j
@RestController
@RequestMapping("/progressInfo")
public class ProgressInfoController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private ProgressInfoService progressInfoService;

    /**
     * 分页查询所有dto数据
     *
     * @param requestModel 查询分页对象
     * @return 所有数据
     */
    @PostMapping("/selectDtoPage")
    public RestMessage selectDtoPage(@RequestBody RequestModel<ProgressInfoVo> requestModel) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<ProgressInfoDTO> record = progressInfoService.selectDtoPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder(record).build();
    }

    /**
     * 进度详情
     *
     * @param id 主键
     * @return 统一出参
     */
    @GetMapping("/progressDetail/{id}")
    public RestMessage progressDetail(@PathVariable Long id) {
        Assert.notNull(id, "id 不能为空");
        return progressInfoService.progressDetail(id);
    }

    /**
     * 新增计划任务
     *
     * @param progressInfoVo 实体对象
     * @return 新增结果
     */
    @PostMapping("/addProgress")
    public RestMessage addProgress(@RequestBody ProgressInfoVo progressInfoVo) {
        return progressInfoService.addProgress(progressInfoVo);
    }

    /**
     * 修改进度数据
     *
     * @param progressInfoVo 实体对象vo
     * @return 修改结果
     */
    @PutMapping("/updateProgress")
    public RestMessage updateProgress(@RequestBody ProgressInfoVo progressInfoVo) {
        Assert.notNull(progressInfoVo.getId(), "id 不能为空");
        return progressInfoService.updateProgress(progressInfoVo);
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public RestMessage delete(@RequestParam("idList") List<Long> idList) {
        Assert.notEmpty(idList, "未选择删除数据");
        return RestBuilders.successBuilder().success(this.progressInfoService.deleteByIds(idList)).build();
    }

    /**
     * 根据进度id获取关联模型信息
     * @param progressId
     * @return
     */
    @GetMapping("/getJoinModel/{progressId}")
    public RestMessage getJoinModel(@PathVariable("progressId") Long progressId){
        return RestBuilders.successBuilder().success(true).data(this.progressInfoService.getJoinModel(progressId)).build();
    }

    /**
     * 关联模型
     *
     * @param progressInfoVo 进度信息vo
     * @return 统一出参
     */
    @PostMapping("/joinModel")
    public RestMessage joinModel(@RequestBody ProgressInfoVo progressInfoVo) {
        Assert.notNull(progressInfoVo.getId(), "id 不能为空");
        //Assert.notEmpty(progressInfoVo.getProgressIntegrateRefList(), "未选择关联模型");
        return progressInfoService.joinModel(progressInfoVo);
    }

    /**
     * 获取进度任务模型树
     *
     * @param progressInfoVo 进度信息vo
     * @return 统一出参
     */
    @PostMapping("/getProgressModelTree")
    public RestMessage getProgressModelTree(@RequestBody ProgressInfoVo progressInfoVo) {
        Assert.notNull(progressInfoVo.getId(), "id 不能为空");
        return progressInfoService.getProgressModelTree(progressInfoVo);
    }

    /**
     * 获取进度任务模型
     *
     * @param progressInfoVo 进度信息vo
     * @return 统一出参
     */
    @PostMapping("/getProgressModel")
    public RestMessage getProgressModel(@RequestBody ProgressInfoVo progressInfoVo) {
        Assert.notNull(progressInfoVo.getId(), "id 不能为空");
        return progressInfoService.getProgressModel(progressInfoVo);
    }

    /**
     * 导入计划
     *
     * @param file       excel文件
     * @param progressId 进度id
     * @return 统一出参
     * @throws Exception 异常
     */
    @PostMapping("/importProgressTask/{progressId}")
    public RestMessage importProgressTask(@NotNull @RequestPart("file") MultipartFile file, @PathVariable Long progressId) throws Exception {
        Assert.notNull(progressId, "进度id不能为空");
        return progressInfoService.importProgressTask(file, progressId);
    }

    /**
     * 修改关键节点标记
     *
     * @param progressInfoVo 实体对象vo
     * @return 修改结果
     */
    @PutMapping("/pointMark")
    public RestMessage pointMark(@RequestBody ProgressInfoVo progressInfoVo) {
        Assert.notNull(progressInfoVo.getId(), "id 不能为空");
        Assert.notNull(progressInfoVo.getPoint(), "是否关键节点标记不能为空");
        return progressInfoService.pointMark(progressInfoVo);
    }

    /**
     * 下载模板
     *
     * @param request  请求
     * @param response 响应
     */
    @GetMapping("/exportTemplate")
    public void exportTemplate(HttpServletRequest request, HttpServletResponse response) {
        String title = "进度计划";
        String fileName = title + ".xlsx";
        try {
            OutputStream outputStream = OutputStreamUtil
                    .getOutputStream(request, response, fileName);
            String keyValue = "大纲级别(必填*):code,任务名称(必填*):name,工期(天)(必填*):totalWork,"
                    + "计划开始(必填*):startTime,计划结束(必填*):endTime,前置任务:beforeWork";
            ExcelTools
                    .exportExcel(outputStream, keyValue, ProgressInfoVo.creatExcel(), ExcelConstant.XLSX, title);
            response.flushBuffer();
            outputStream.close();
        } catch (IOException e) {
            throw new RuntimeException("excel导出失败！IOException异常");
        } catch (Exception e) {
            throw new RuntimeException("excel导出失败！");
        }
    }

    /**
     * 大屏进度计划任务信息
     *
     * @param requestModel 查询分页对象
     * @return 统一出参
     */
    @PostMapping("/screenProgressTaskInfo")
    public RestMessage screenProgressTaskInfo(@RequestBody RequestModel<ProgressInfoVo> requestModel) {
        //Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<ScreenProgressInfoDTO> record = progressInfoService.screenProgressTaskInfo(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder(record).build();
    }



}

